from __future__ import with_statement
from contextlib import closing

import sqlite3

from flask import Flask, request, session, g, redirect, url_for, \
     abort, render_template, flash

app = Flask(__name__)
app.config.from_pyfile('picm.cfg', silent=False)

def connect_db():
	return sqlite3.connect(app.config['DATABASE'])

def init_db():
	with closing(connect_db()) as db:
		with app.open_resource(app.config['SCHEMA']) as f:
			db.cursor().executescript(f.read())
		db.commit()

@app.before_request
def before_request():
	g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
	g.db.close()

@app.route('/')
def index():
	return render_template('index.html')

if __name__ == '__main__':
	app.run(host=app.config['IP_ADDRESS'], port=app.config['PORT'], debug=app.config['DEBUG'])

